package com.sgb.link.utils;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import com.swgk.core.customview.ItemInputTextView;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

/**
 * Time:2021/8/4
 * AUTHOR:F_Q
 * Description:
 */
public class NumericalTransformationTool {


    //集采发布EdText处理
    public static void setMonitor(EditText gratisRepairTime) {
        gratisRepairTime.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String text = s.toString();
                int length = text.length();
                if (length ==1&&text.equals("0")){
                    s.clear();
                }
            }
        });
    }

    //生成历史list
    public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(src);
        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream in = new ObjectInputStream(byteIn);
        @SuppressWarnings("unchecked")
        List<T> dest = (List<T>) in.readObject();
        return dest;
    }

    /**
     * ItemInputTextView  监听
     * @param initialEnergy
     */
    public static void OnItemInputTextViewChange(ItemInputTextView initialEnergy) {
        initialEnergy.onTextChange(new ItemInputTextView.onTextChange() {
            @Override
            public void change(String s) {
                int length = s.length();
                if (length == 1 && s.equals("0")) {
                    initialEnergy.setContent("");
                }
            }
        });
    }

    /**
     * 数字转换成汉子
     * @param position
     */
    public static String convert(int position) {
        String text = String.valueOf(position);
        StringBuilder sb = new StringBuilder();
        char[] chars = text.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            String pos = String.valueOf(text.toCharArray()[i]);
            sb.append(getNumberText(pos));
        }
        return sb.toString();
    }

    private static String getNumberText(String pos){
        switch (Integer.parseInt(pos)){
            case 1:
                return "一";
            case 2:
                return "二";
            case 3:
                return "三";
            case 4:
                return "四";
            case 5:
                return "五";
            case 6:
                return "六";
            case 7:
                return "七";
            case 8:
                return "八";
            case 9:
                return "九";
            case 0:
                return "零";
        }
        return "";
    }

    public static String getNumberText(int pos){
        switch (pos){
            case 1:
                return "一";
            case 2:
                return "二";
            case 3:
                return "三";
            case 4:
                return "四";
            case 5:
                return "五";
            case 6:
                return "六";
            case 7:
                return "七";
            case 8:
                return "八";
            case 9:
                return "九";
            case 10:
                return "十";
        }
        return "";
    }
}
